﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using AzukiMap.Tools.Logger;

namespace AzukiMapUI
{
    public delegate bool FormInputChecker(string formString);

    public static class FormInputControl
    {
        public static string EOL_IN_FORM = "\r";
        //static string[] listSplitter = new string[]{EOL_IN_FORM};

        /*
        public FormInputControl()
        {
            listSplitter = new string[]{EOL_IN_FORM};
        }
         */

        public static bool CheckListData(string formString, FormInputChecker checker)
        {
            if (formString == null || formString == "")
            {
                return true;
            }

            string[] splittedStrs = SplitString(formString);

            // check string in text form.
            bool noProblem = true;
            foreach (string str in splittedStrs)
            {
                if (! checker(str))
                {
                    noProblem = false;
                    Logger.Warn("'" + str + "' is not a valid string.\n");
                }
            }
            return noProblem;
        }

        static string[] SplitString(string formString)
        {
            string[] listSplitter = new string[] { EOL_IN_FORM };
            return formString.Split(listSplitter, StringSplitOptions.RemoveEmptyEntries);
        }

        public static void SetListDataFromForm(List<string> list, string formString)
        {
            if (formString == null)
            {
                list.Clear();
                return;
            }

            string[] splittedStrs = SplitString(formString);

            list.Clear();
            foreach (string str in splittedStrs)
            {
                list.Add(str);
            }

        }
        public static void SetListDataFromForm(List<double> list, string formString)
        {
            if (formString == null)
            {
                list.Clear();
                return;
            }

            string[] splittedStrs = SplitString(formString);

            // if there is no error string it sets data from a form.
            list.Clear();
            foreach (string str in splittedStrs)
            {
                list.Add(double.Parse(str));
            }
        }

        public static void ShowElement(UIElement element)
        {
            element.Visibility = Visibility.Visible;
        }

        public static void HideElement(UIElement element)
        {
            element.Visibility = Visibility.Collapsed;
        }

        public static bool UriStringChecker(string inputString)
        {
            try
            {
                Uri testUri = new Uri(inputString);
            }
            catch (UriFormatException ex)
            {
                return false;
            }
            catch (ArgumentNullException ex)
            {
                return false;
            }
            return true;
        }

        public static bool DoubleStringChecker(string inputString)
        {
            try
            {
                double testDouble = double.Parse(inputString);
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public static bool IntStringChecker(string inputString)
        {
            try
            {
                int testInt = int.Parse(inputString);
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public static bool NoCheck(string inputString)
        {
            return true;
        }

        public static bool HasListData(string inputString)
        {
            if (! HasData(inputString))
            {
                return false;
            }
            string[] listSplitter = new string[] { EOL_IN_FORM };
            string[] splittedStr = inputString.Split(listSplitter, StringSplitOptions.RemoveEmptyEntries);
            if (splittedStr.Length == 0)
            {
                return false;
            }
            return true;
        }

        public static bool HasData(string inputString)
        {
            if (inputString == null || inputString == "")
            {
                return false;
            }
            return true;
        }

        public static bool IsPositiveString(string inputString)
        {
            return IsPositiveString(inputString, false);
        }

        public static bool IsPositiveString(string inputString, bool includeZero)
        {
            double testDouble;
            if (inputString == null || inputString == "")
            {
                return false;
            }
            try
            {
                testDouble = double.Parse(inputString);
            }
            catch (Exception ex)
            {
                return false;
            }
            if (testDouble > 0.0 || ( includeZero && testDouble >= 0))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool CheckUriList(string inputString, UIElement warnIcon)
        {
            FormInputChecker checker = new FormInputChecker(FormInputControl.UriStringChecker);
            if (!HasListData(inputString))
            {
                ShowElement(warnIcon);
                return false;
            }

            if (FormInputControl.CheckListData(inputString, checker))
            {
                HideElement(warnIcon);
                return true;
            }
            else
            {
                ShowElement(warnIcon);
                return false;
            }
        }

        public static bool CheckDoubleList(string inputString, UIElement warnIcon)
        {
            FormInputChecker checker = new FormInputChecker(FormInputControl.DoubleStringChecker);
            if (!HasListData(inputString))
            {
                ShowElement(warnIcon);
                return false;
            }
            if (CheckListData(inputString, checker))
            {
                HideElement(warnIcon);
                return true;
            }
            else
            {
                ShowElement(warnIcon);
                return false;
            }
        }

        public static bool CheckPositiveDouble(string inputString, UIElement warnIcon, bool includeZero)
        {
            if (IsPositiveString(inputString, includeZero))
            {
                HideElement(warnIcon);
                return true;
            }
            else
            {
                ShowElement(warnIcon);
                return false;
            }
        }

        public static bool CheckPositiveDouble(string inputString, UIElement warnIcon)
        {
            return CheckPositiveDouble(inputString, warnIcon, false);
        }

        public static bool CheckNaturalNumber(string inputString, UIElement warnIcon)
        {
            if (! IsPositiveString(inputString))
            {
                ShowElement(warnIcon);
                return false;
            }
            if (IntStringChecker(inputString))
            {
                HideElement(warnIcon);
                return true;
            }
            else
            {
                ShowElement(warnIcon);
                return false;
            }
        }

        public static bool CheckNumberBetween(string inputString, UIElement warnIcon, double fromValue, double toValue)
        {
            if ( ! DoubleStringChecker(inputString)) {
                ShowElement(warnIcon);
                return false;
            }
            double val = double.Parse(inputString);
            if (fromValue <= val && val <= toValue)
            {
                HideElement(warnIcon);
                return true;
            }
            ShowElement(warnIcon);
            return false;
        }

        public static bool CheckUri(string inputString, UIElement warnIcon)
        {
            if (! HasData(inputString))
            {
                ShowElement(warnIcon);
                return false;
            }
            if (UriStringChecker(inputString))
            {
                HideElement(warnIcon);
                return true;
            }
            else
            {
                ShowElement(warnIcon);
                return false;
            }
        }

        public static bool HasData(string inputString, UIElement warnIcon)
        {
            if (HasData(inputString))
            {
                HideElement(warnIcon);
                return true;
            }
            ShowElement(warnIcon);
            return false;
        }

        public static string GetSelectedTextFromCombo(ComboBox combo)
        {
            return ((TextBlock)combo.SelectedItem).Text;
        }

        public static void SetForm(TextBox textBox, List<string> list)
        {
            string textToSet = "";
            foreach (string perm in list)
            {
                textToSet += perm;
                textToSet += FormInputControl.EOL_IN_FORM;
            }
            textBox.Text = textToSet;
        }

        public static void SetForm(TextBox textBox, List<double> list)
        {
            string textToSet = "";
            foreach (double perm in list)
            {
                textToSet += perm.ToString();
                textToSet += FormInputControl.EOL_IN_FORM;
            }
            textBox.Text = textToSet;
        }


        public static void SetForm(TextBox textBox, String value)
        {
            value = (value == null) ? "" : value;
            textBox.Text = value;
        }

        public static void SetForm(TextBox textBox, int value)
        {
            value = (value == null) ? 0 : value;
            textBox.Text = value.ToString();
        }

        public static void SetForm(TextBox textBox, double value)
        {
            value = (value == null) ? 0 : value;
            textBox.Text = value.ToString();
        }

        public static void SetForm(CheckBox check, bool value)
        {
            value = (value == null) ? false : value;
            check.IsChecked = value;
        }

        public static void SetForm(ComboBox combo, int value)
        {
            try
            {
                combo.SelectedIndex = value;
            }
            catch (Exception ex)
            {
                combo.SelectedIndex = -1;
            }
        }
    }
}
